<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('会员等级列表')"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="case-form">
                <div class="select-list">
                    <ul>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">

        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<script th:inline="javascript">
    var prefix = ctx + "back/memberVipType";
    const editFlag = [[${@permission.hasPermi('back:memberVipType:edit')}]];
    const removeFlag = [[${@permission.hasPermi('back:memberVipType:remove')}]];

    const yesNoDict = [[${@dict.getType('is_enable')}]]

    $(function () {
        var options = {
            url: prefix + "/searchList",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            modalName: "会员等级",
            sortName: "orderNum",
            sortOrder: "asc",
            onEditableSave: onEditableSave,
            columns: [
                {
                    field: 'id',
                    title: "等级ID",
                    sortable: true
                },
                {
                    field: 'vipTypeName',
                    title: '等级名称',
                    sortable: true
                },
                {
                    field: 'isSystem',
                    title: '是否系统',
                    sortable: true,
                    formatter: function (value, item, index) {
                        return $.table.selectDictLabel(yesNoDict, value);
                    }
                },
                {
                    field: 'upgradeCondition',
                    title: '升级方式',
                    sortable: true,
                    visible: false,
                    editable: function(value, row, index) {
                        if(1 != row.id){
                            return  {
                                type: 'select',
                                title: '升级方式',
                                source: [{
                                    value: 0,
                                    text: "累计充值金额"
                                }, {
                                    value: 1,
                                    text: "累计加油金额（微信支付）"
                                },{
                                    value: 2,
                                    text: "累计消费金额（累计充值+累计加油）"
                                }]

                            }
                        }else{
                            return {
                                disabled:true,
                                type: 'select',
                                title: '',
                                source: [{
                                    value: 0,
                                    text: "默认等级无需升级"
                                }]
                            }
                        }
                    }
                },
                {
                    field: 'upgradeValue',
                    title: '升级金额',
                    sortable: true,
                    visible: false,
                    editable: function(value, row, index) {
                        if(1 != row.id){
                            return  {
                                type: 'text',
                                title: '升级金额',
                                emptytext: "【升级金额】不能为空",
                                validate: function (v) {
                                    if ($.trim(v) == '') {
                                        return '【升级金额】不能为空!';
                                    }
                                    if (isNaN(v)) {
                                        return '【升级金额】必须是数字';
                                    }
                                    if (!(/(^[0-9]\d*$)/.test(v))) {
                                        return '请输入正确的【升级金额】';
                                    }
                                }
                            }
                        }else{
                            return {
                                disabled:true,
                                type: 'select',
                                title: '',
                                source: [{
                                    value: 0,
                                    text: "0"
                                }]
                            }
                        }
                    }
                },
                {
                    field: 'isEnable',
                    title: '状态',
                    sortable: true,
                    editable: function(value, row, index) {
                        if(1 != row.id){
                            return  {
                                type: 'select',
                                title: '状态',
                                disabled:true,
                                source: [{
                                    value: 1,
                                    text: "正常"
                                }, {
                                    value: 0,
                                    text: "停用"
                                }]
                            }
                        }else{
                            return {
                                disabled:true,
                                type: 'select',
                                title: '',
                                source: [{
                                    value: 1,
                                    text: "正常"
                                }]
                            }
                        }
                    }
                },
                {
                    field: 'orderNum',
                    title: '排序',
                    sortable: true,
                    editable: {
                        disabled:true,
                        type: 'text',
                        title: '排序',
                        emptytext: "【排序】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【排序】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【排序】必须是数字';
                            }
                            if (!(/(^[0-9]\d*$)/.test(v))) {
                                return '请输入正确的【排序】';
                            }
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        // actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit">编辑</i></a> ');
                        if(0 == row.isSystem){
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-trash"></i>删除</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });
    function onEditableSave(field, row) {
        let updateData = {};
        updateData[field] = row[field];
        updateData.id = row.id;
        $.ajax({
            type: 'post',
            url: ctx + "back/memberVipType/edit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("修改成功");
                } else {
                    $.modal.alertError(result.msg);
                }
                $.table.refresh();
            }
        });
    }
</script>
</body>
</html>